<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 * @version     $Id: Front.php 140 2010-04-15 08:41:37Z thuan.nguyen $
 */

class Fete_Front
{
    protected
        $_actionDir = null,
        $_viewDir   = null,
        $_theme     = 'default',
        $_outputHandler = null,
        $_view      = null,
        $_baseUrl = null
    ;

    static protected $_instance = null;

    /**
     *
     * @return Fete_Front
     */
    static public function &getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     *
     * @return Fete_Front
     */
    public function &setActionDir($directory)
    {
        $this->_actionDir = $directory;
        return $this;
    }

    /**
     *
     * @return Fete_Front
     */
    public function &setViewDir($directory)
    {
        $this->_viewDir = $directory;
        return $this;
    }

    /**
     *
     * @return Fete_Front
     */
    public function &setTheme($theme)
    {
        $this->_theme = $theme;
        return $this;
    }

    /**
     *
     * @return Fete_View
     */
    public function &getView()
    {
        if (null === $this->_view) {
            $this->_view = new Fete_View();
        }
        return $this->_view;
    }

    /**
     * @return Fete_Front
     */
    public function &setBaseUrl($baseUrl = null)
    {
        if (null === $baseUrl) {
            $this->_baseUrl = substr(
                $_SERVER['SCRIPT_NAME'],
                0,
                strrpos($_SERVER['SCRIPT_NAME'], '/')
            );
            return $this;
        }

        $this->_baseUrl = $baseUrl;

        return $this;
    }

    /**
     * Get base url of website
     * @return string
     */
    public function getBaseUrl()
    {
        if (null === $this->_baseUrl) {
            $this->setBaseUrl();
        }

        return $this->_baseUrl;
    }

    /**
     *
     * @return Fete_Request
     */
    public function getRequest()
    {
        return Fete_Request::getInstance();
    }

    public function run()
    {
        if (null === $this->_actionDir) {
            throw new Fete_Exception('Action directory is not set');
            return true;
        }

        if (null === $this->_viewDir) {
            throw new Fete_Exception('View directory is not set');
            return true;
        }

        set_include_path(
            get_include_path() . PATH_SEPARATOR
            . $this->_actionDir . PATH_SEPARATOR
            . $this->_viewDir);

        $this->_view = new Fete_View();

        @session_start();
        ob_start($this->_outputHandler);
        $action = Fete_Request::getInstance()->init()->getAction();

        if (false !== strpos($action, '/')
            || false !== strpos($action, '\\')
            || false !== strpos($action, '.')
        ) {
            throw new Fete_Exception('Invalid action: ' . $action, 403);
            return false;
        }

        $viewScript = $this->_viewDir . '/' . $this->_theme . '/' . $action
        . '.tpl';

        if (!file_exists($viewScript)) {
            throw new Fete_Exception('Action does not exist: ' . $action, 404);
            return false;
        }

        require $viewScript;

        ob_end_flush();
    }

    public function __call($name, $args)
    {
        return $this->getView()->__call($name, $args);
    }

    protected function _getParam($key)
    {
        return Fete_Request::getInstance()->getParam($key);
    }

    protected function _redirect($url)
    {
        header('Location: ' . $url);
        exit;
    }

    protected function __construct() {}

    private function __clone() {}
}